package com.wolfpire.workflow.cmd;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.jbpm.api.JbpmException;
import org.jbpm.api.cmd.Environment;
import org.jbpm.pvm.internal.cmd.AbstractCommand;
import org.jbpm.pvm.internal.task.ParticipationImpl;
import org.jbpm.pvm.internal.task.TaskImpl;

/**
 * 代办选择清空参与人
 * @author linRJ
 *
 */
public class RemoveParticipationCmd extends AbstractCommand<Object> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4037344522275350417L;
	
	protected String taskId;
    protected String swimlaneId;
    protected String userId;
    protected String groupId;
    protected String type;

    public RemoveParticipationCmd(String taskId, String swimlaneId, String userId, String groupId, String type) {
        this.taskId = taskId;
        this.swimlaneId = swimlaneId;
        this.userId = userId;
        this.groupId = groupId;
        this.type = type;
    }

    
	public Object execute(Environment environment) throws Exception {
		Session session = environment.get(Session.class);

		if (taskId != null) {
			TaskImpl task = (TaskImpl) session.get(TaskImpl.class,
					Long.parseLong(taskId));
			if (task == null) {
				throw new JbpmException("task " + taskId + " was not found");
			}

			Set<ParticipationImpl> pts = task.getParticipations();
			
			List<ParticipationImpl> delList = new ArrayList<ParticipationImpl>();
			
			Iterator<ParticipationImpl> it = pts.iterator();
		    while (it.hasNext()){
		    	ParticipationImpl pt =  it.next();
		    	delList.add(pt);
			 }
		    
		    for (ParticipationImpl participationImpl : delList) {
		    	task.removeParticipant(participationImpl);
			}
		
		}

		return null;
	}

}
